﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Dossier_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void nieuwpatient_Click(object sender, EventArgs e)
    {
        Panel2.Visible = false;
        Panel1.Visible = true;
    }

    protected void annuleer_Click(object sender, EventArgs e)
    {
        initialen.Text = "";
        achternaam.Text = "";
        straatennummer.Text = "";
        postcode.Text = "";
        woonplaats.Text = "";
        telefoonnummer.Text = "";
        geboortedatum.Text = "";
        geslacht.Text = "";
        zorgverzekeraar.ClearSelection();
        polisnummer.Text = "";
        Panel1.Visible = false;
    }

    protected void slaop_Click(object sender, EventArgs e)
    {
        // connection
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\yuix\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\mmcinfo.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";

        try
        {// update db
            int PID = Int32.Parse(geenresultaat.Text); // PID

            // open connection
            con.Open();
            // instantiate a new command with query update and connection
            SqlCommand cmd = new SqlCommand("UPDATE Patient SET Initialen = " + initialen.Text + ", Achternaam = " + achternaam.Text + ", StraatEnNummer = " + straatennummer.Text + ", Postcode = " + postcode.Text + ", Woonplaats = " + woonplaats.Text + ", Telefoonnummer = " + telefoonnummer.Text + ", Geboortedatum = " + geboortedatum.Text + ", Geslacht = " + geslacht.Text + ", ZVID = " + zorgverzekeraar.Text + ", Polisnummer = " + polisnummer.Text + " WHERE PATID = "+ PID +"", con);
        }
        catch (FormatException)
        {// insert db
            SqlDataSource1.Insert();
        }

    }

    protected void patientzoeken_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false; // invoer panel
        Panel2.Visible = true; // zoek panel
    }

    /*
     * Zoek knop
     */
    protected void Button1_Click(object sender, EventArgs e)
    {
        // geboortedatum input from search (dd-MM-yyyy)
        String geboortedatum = zoekGD.Text;

        // connection
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\yuix\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\mmcinfo.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";

        try
        {
            // open connection
            con.Open();
            // instantiate a new command with query and connection
            SqlCommand cmd = new SqlCommand("SELECT * FROM Patient WHERE Geboortedatum = '" + geboortedatum + "'" , con);
            // call execute reader to get query results
            SqlDataReader rdr = cmd.ExecuteReader();

            int i = 0; // dropdownlist position index
            int PID = 0; // patient id
            // add records to dropdownlist
            while (rdr.Read())
            {
                DropDownList1.Items.Insert(i, rdr.GetString(2));

                // get PID from first result
                if(PID == 0)
                    PID = rdr.GetInt32(0);
            
                i++;
            }

            // show dropdownlist
            if (i > 0)
            {
                DropDownList1.Visible = true; // dropdownlist with found patients
                Panel1.Visible = true; // patient form enable
                loadPatient(PID); // load patient in form
                geenresultaat.Text = "PID: " + PID;
                geenresultaat.Visible = false;
            }
            // error message if no results
            else
            {
                geenresultaat.Text = "Er zijn geen patienten gevonden met uw gewenste geboortedatum";
            }
        }
        finally
        {
            // close the connection
            if (con != null)
            {
                con.Close();
            }
        }                
    }

    protected void loadPatient(int id)
    {
        // connection
        SqlConnection con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\yuix\\Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\mmcinfo.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";

        try
        {
            con.Open();

            // instantiate a new command with query and connection
            SqlCommand cmd = new SqlCommand("SELECT * FROM Patient WHERE PATID = '" + id + "'", con);
            // call execute reader to get query results
            SqlDataReader rdr = cmd.ExecuteReader();

            // fill form with patient info
            while (rdr.Read())
            {
                initialen.Text = rdr.GetString(1);
                achternaam.Text = rdr.GetString(2);
                straatennummer.Text = rdr.GetString(3);
                postcode.Text = rdr.GetString(4);
                woonplaats.Text = rdr.GetString(5);
                telefoonnummer.Text = rdr.GetString(6);
                geboortedatum.Text = rdr.GetString(7);
                geslacht.Text = rdr.GetString(8);
                // zorgverzekering mist
                polisnummer.Text = "" + rdr.GetInt32(10);
            }

            // set form enabled to false
            enableForm(false);

            // show lab / dosier links
            LinkDosier.NavigateUrl = ResolveUrl("~/Dosier/Dosier.aspx?PID="+id);
            LinkLab.NavigateUrl = ResolveUrl("~/Dosier/Labaanvraag.aspx?PID=" + id);
            Panel3.Visible = true;
            
            // hide save button
            slaop.Visible = false;
            wijzig.Visible = true;
        }
        finally
        {
            // close the connection
            if (con != null)
            {
                con.Close();
            }
        }
    }

    protected void enableForm(Boolean enable)
    {
        initialen.Enabled = enable;
        achternaam.Enabled = enable;
        straatennummer.Enabled = enable;
        postcode.Enabled = enable;
        woonplaats.Enabled = enable;
        telefoonnummer.Enabled = enable;
        geboortedatum.Enabled = enable;
        geslacht.Enabled = enable;
        zorgverzekeraar.Enabled = enable;
        polisnummer.Enabled = enable;
    }

    protected void wijzig_Click(object sender, EventArgs e)
    {
        enableForm(true); // set forms enabled
        slaop.Visible = true; // submit button
        wijzig.Visible = false; // wijzig button
        Panel3.Visible = false; // link panel
    }
}